﻿/********************************************************************
	created:	2011/05/29
	created:	29:5:2011   21:06
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client\ClientCallbackFunction.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client
	file base:	ClientCallbackFunction
	file ext:	cs
	author:		
	
	purpose:	客户端数据处理功能
*********************************************************************/
using System;
using CommonLibrary;

namespace Client
{
    internal static class ClientCallbackFunction
    {
        #region 登录处理

        internal static void SignInProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var signinData =
                Functions.DeserializeFromBytes<Protocol.SignIn>(envelope.ByteMessage);

            if (signinData == null) return;
            if (signinData.Result)
            {
                MainWindow.MainWindowThisUser.MyName = signinData.MyName;
                MainWindow.MainWindowThisUser.MyUid = signinData.MyUid;
                MainWindow.MainWindowThisUser.IsLogin = signinData.Result;
            }
            else
            {
                MainWindow.MainWindowThisUser.MyUid = signinData.MyUid;
                MainWindow.MainWindowThisUser.IsLogin = signinData.Result;
            }
        }

        #endregion

        #region 注册处理

        internal static void RegNewUserProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var registerData =
                Functions.DeserializeFromBytes<Protocol.RegNewUser>(envelope.ByteMessage);

            if (registerData == null) return;
            if (registerData.Result)
            {
                string regResponsesMessage = "注册成功 您的ID为：" + registerData.MyUid + Environment.NewLine + "您的昵称为：" +
                                             registerData.MyName + Environment.NewLine + "请记好您的ID和密码";
                MainWindow.SyncContext.Post(msg => MainWindow.ThisRegWindow.RegisterReponses(regResponsesMessage), null);
            }
            else
            {
                const string regResponsesMessage = "非常抱歉，注册失败了，请重试";
                MainWindow.SyncContext.Post(msg => MainWindow.ThisRegWindow.RegisterReponses(regResponsesMessage), null);
            }
        }

        #endregion

        #region 查询所有在线用户ID

        internal static void QueryAllOnlineUserProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var usersInfo =
                Functions.DeserializeFromBytes<Protocol.GetOnlineUsers>(
                    CryptoClient.DesDecrypt(MainWindow.MainWindowThisUser.DesKey, MainWindow.MainWindowThisUser.DesIv,
                                            envelope.ByteMessage));

            if (usersInfo == null) return;

            MainWindow.MainWindowThisUser.UsersInfos = usersInfo.UserInfos;
        }

        #endregion

        #region 接收聊天消息

        internal static void TalkingProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var talkingPackage =
                Functions.DeserializeFromBytes<Protocol.TalkingPackage>(
                    CryptoClient.DesDecrypt(MainWindow.MainWindowThisUser.DesKey, MainWindow.MainWindowThisUser.DesIv,
                                            envelope.ByteMessage));

            if (talkingPackage == null) return;

            ClientChatForm chatform = MainWindow.ThisClientChatForm.GetClientChatForm(talkingPackage.MyUid);
            if (chatform == null)
            {
                MainWindow.NewChatForm(out chatform, MainWindow.MainWindowThisUser.MyUid, talkingPackage.Sender,
                                       MainWindow.MainWindowThisUser.MyName, talkingPackage.SenderName);
            }
            MainWindow.SyncContext.Post(
                add =>
                chatform.AddNewMessage(talkingPackage.SenderName, talkingPackage.Message, talkingPackage.SendTime), null);
        }

        #endregion

        #region DES密钥更新请求

        internal static void DesKeyUpdateProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var desKeyUpdateInfo =
                Functions.DeserializeFromBytes<Protocol.DesKeyUpdate>(envelope.ByteMessage);

            if (desKeyUpdateInfo == null) return;

            if (desKeyUpdateInfo.ServerAnswer)
            {
                string key;
                string iv;
                var desKeyUpdateMessage = new Protocol.DesKeyUpdateAck();
                CryptoClient.DesGenerateKey(out key, out iv);
                desKeyUpdateMessage.AckIv = iv;
                desKeyUpdateMessage.AckKey = key;
                desKeyUpdateMessage.MyName = MainWindow.MainWindowThisUser.MyName;
                desKeyUpdateMessage.MyUid = MainWindow.MainWindowThisUser.MyUid;
                byte[] messageSend = CommonFunctionClass.SealMessageToEnvelope(desKeyUpdateMessage,
                                                                               desKeyUpdateMessage.MyUid, false,
                                                                               false);
                MainWindow.SendDataDelegateFunc.BeginInvoke(messageSend, null, null);
            }
        }

        #endregion

        #region DES密钥更新请求确认

        internal static void DesKeyUpdateAckProcessdure(byte[] byteMessage)
        {
            var envelope = Functions.DeserializeFromBytes<Protocol.MessageEnvelope>(byteMessage);

            var desKeyUpdateInfo =
                Functions.DeserializeFromBytes<Protocol.DesKeyUpdateAck>(envelope.ByteMessage);

            if (desKeyUpdateInfo == null) return;

            if (desKeyUpdateInfo.ServerAnswer)
            {
                MainWindow.MainWindowThisUser.DesKey = desKeyUpdateInfo.AckKey;
                MainWindow.MainWindowThisUser.DesIv = desKeyUpdateInfo.AckIv;
                MainWindow.MainWindowThisUser.IsUpdateKey = true;
                MainWindow.MainWindowThisUser.GetUsersInfo = true;
            }
        }

        #endregion
    }
}